$(function(){
	
	var info_weekly_nba=[];
	var info_weekly_epl=[];
	var now = new Date();
	var date= now.getDate();
	var count=0;
	var print_day = now.getDate()+7;
	var datelist =now.getDate();

	setLocalList('epl');
	setLocalList('nba');
	//var ajax_url = "http://70.12.110.72:3000";
	
	$('#btnNba').on('click',function(){   
		
		$.mobile.showPageLoadingMsg("d","경기목록 로딩중");	
		$.ajax({
	         url : ajax_url+'/new_schedule',
	         data : {url : 'http://sports.media.daum.net/basketball/nba/schedule/main.daum?game_year=2013&game_month=12'},
	         complete: function(){
	        	 $.mobile.hidePageLoadingMsg();
	         },
	         success : function(data){
	          var realrank =$(data).find('tbody');
	    	  $('tbody').append('<tr>');
	         
	         realrank.find('.today, .fast').each(function(idx,item){
	        	 
	        	var text_date =$(item).find('.date').text();  
	           	var text_num = parseInt(text_date);
	        
	           	if(text_num>print_day) return;
	           
	          	var text_team =$(item).find('.team1, .vs, .team2').text();
	          	var text_team2 =$(item).find('.team').text();
               	var text_time =$(item).find('.time').text();
               	var text_place =$(item).find('.stadium').text();               	
           

               	if(text_team){
               		console.log('pastgame');
               		text_date = " ";
               		text_team2 = " ";
               		text_time = " ";
               		text_place = " ";
               	}else{
               		console.log('fastgame');
               		var ul=$('#tableNba');
               		if(text_team2){
               			$('<li>'+text_date+text_team2+text_time+'<input type="checkbox" name="chkNba"/></li>').appendTo(ul);	
	           		}
	           		//console.log(text_date);
               	}       
         	  });	            
	         }
	      });	      
	   });
		

	$('#enrollNba').on('click',function(){
	
		var chkNba = document.getElementsByName("chkNba");
		for(var i=0; i<chkNba.length; i++){
			if(chkNba[i].checked==true){
				info_weekly_nba[i]= $('#tableNba').children().eq(i).text();
				alert(info_weekly_nba[i]);
			}
			chkNba[i].checked=false;
			chkNba[i].disabled=true; 
			}		
			
		setNbaList();
	});
	
	$('#btnEpl').on('click',function(){
		
		var print_year = now.getYear();
		var print_month = now.getMonth();
		
		var url_year = '?game_year='+(print_year+1900);
		var url_month = '&game_month='+(print_month+1);
	
		var url_total = 'http://sports.media.daum.net/worldsoccer/all/schedule/main.daum'+ url_year + url_month;

		$.mobile.showPageLoadingMsg("d","경기목록 로딩중");	
		$.ajax({
			
			url : ajax_url+'/new_schedule',
			
	        data : {url : url_total},
	        complete: function(){
	        	 $.mobile.hidePageLoadingMsg();
	         },
	        success : function(data){
			
	        	//console.log('data : '+data);
	          var realrank =$(data).find('tbody');
	    	
	         $('tbody').append('<tr>');
	         
	         realrank.find('.today, .fast').each(function(idx,item){
	        	
	        	var text_date =$(item).find('.date').text();  
	           	var text_num = parseInt(text_date);
	           	
	           	
	           	if(text_num>print_day) return;
	           
	          	var text_team =$(item).find('.team1, .vs, .team2').text(); //占쏙옙키占쏙옙占?
               	var text_team2 =$(item).find('.team').text(); //占싱뤄옙占쏙옙占쏙옙
               	var text_time =$(item).find('.time').text().substring(0,5);
               	var text_place =$(item).find('.stadium').text();
               	
               	             		
               	if(text_team2) {
	           		var ul=$('#tableEpl');	
	           				$('<li>'+text_date+text_team2+text_time+'<input type="checkbox" name="chkEpl"/></li>').appendTo(ul);
	           		           		
	           	}

	           		//console.log(text_date);
       
           });
            
	         }
	      });
		
	      
	});
	//
	$('#enrollEpl').on('click',function(){	
		
		var chkEpl = document.getElementsByName("chkEpl");
		for(var i=0; i<chkEpl.length; i++){
			if(chkEpl[i].checked==true){
			//console.log(this);
			console.log($('#tableEpl').children().eq(i).text());
			info_weekly_epl[i]= $('#tableEpl').children().eq(i).text();
			alert(info_weekly_epl[i]);
			chkEpl[i].checked=false;
			chkEpl[i].disabled=true; 
			}		
		}
		setEplList();
		
	});
	
	
	///
	function setNbaList(){//?ㅺ컪?쇰줈 ?ｋ뒗 怨?
		for(var j=0; j<100;j++){
			if(info_weekly_nba[j]==undefined){
				continue;
			}else{
				localStorage.setItem("nba"+j+"/"+date, info_weekly_nba[j]); // key-value ??????? ????
				var key = $('<a href="#" data-role="button" id="delete" data-icon="delete" data-inline="true" data-iconpos="notext">Delete</a>'); key.data('key', "nba"+j+"/"+date).button();
				//alert(key);
				//console.log(key);
				$("#special_list").append($('<li>' + localStorage.getItem("nba"+j+"/"+date) + '</li>').append(key));
				key.on('click',function(){
					//alert($(this).data('key'));
					localStorage.removeItem($(this).data('key'));
					alert('deleted');
					location.reload();
				});
			}
		}
		location.reload();

	}
	
	function setEplList(){
		for(var j=0; j<100;j++){
			if(info_weekly_epl[j]==undefined){
				continue;
			}else{
				localStorage.setItem("epl"+j+"/"+date, info_weekly_epl[j]); // key-value ??????? ????
				var key = $('<a href="#" data-role="button" id="delete" data-icon="delete" data-inline="true" data-iconpos="notext">Delete</a>'); key.data('key', "epl"+j+"/"+date).button();
				//alert(key);
				//console.log(key);
				$("#special_list").append($('<li>' + localStorage.getItem("epl"+j+"/"+date) + '</li>').append(key));

				key.on('click',function(){
					//alert($(this).data('key'));
					localStorage.removeItem($(this).data('key'));
					alert('deleted');
					location.reload();
						
				});
			}
		}
		location.reload();

	}
	
	//date ?꾩슂 X ajax ?꾩슂
	function setLocalList(evt){
		for(var i=0; i<10; i++){
			var date2=now.getDate()-i;
			if( date2<(date2-7) ) return;//?ㅻ뒛 ?좎쭨蹂대떎 +7???댁쟾?대㈃ 由ы꽩
			else{
				
				for(var j=0; j<100;j++){
					if(localStorage.getItem(evt+j+"/"+date2)==null){
						continue;
					}else{
						if(parseInt(localStorage.getItem(evt+j+"/"+date2))<now.getDate()){
							localStorage.removeItem(evt+j+"/"+date2);
						}else{
							var key = $('<a href="#" data-role="button" id="delete" data-icon="delete" data-inline="true" data-iconpos="notext">Delete</a>'); key.data('key', evt+j+"/"+date2).button();
							$("#special_list").append($('<li>' + localStorage.getItem(evt+j+"/"+date2) + '</li>').append(key));
							key.on('click',function(){
									//alert($(this).data('key'));
									localStorage.removeItem($(this).data('key'));
									alert('deleted');
									location.reload();

							});
						}
						
					}
				}
				
			}
			
		}
		
	}

	
});
